"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 人为因素
Case Name   :  gs_dump导出过程中，执行ctrl+c
Create At   : 2020/10/12
Owner       : @peilinqian
Description :
    1、执行gs_dump，导出postgres数据库全量信息，并对导出文件进行压缩，导出文件格式为sql文本格式
    2、导出过程中执行ctrl+c（kill -2）
    3、重新执行第1步
Expect      :
    1、成功
    2、成功
    3、成功
History     :
    Modified by @peilinqian 2021/10/19:适配2.1.0版本
"""
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys
import datetime

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.ComThread import ComThread

logger = Logger()
common = Common('PrimaryRoot')
commonshpri = CommonSH('PrimaryDbUser')

class ModifyConnection(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')    
    dbStandby1RootNode = Node(node='Standby1Root')    
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    Constant = Constant()
    dump_file_name = os.path.join(macro.DB_INSTANCE_PATH, 'reliability_humanfactor003.sql')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info("-----------reliability_humanfactor003 start-----------")
        

    def test_modify_pidfile(self):
        logger.info("-----------dump--------------------")        
        dump_thread = ComThread(commonshpri.dumpFile, args=(self.dump_file_name, '-F p'))
        dump_thread.start()
        
        logger.info("-----------ctl+c--------------------")       
        common.kill_pid_keyword('gs_dump', '2', self.dbPrimaryUserNode)
        
        dump_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        logger.info("-----------dump fail--------------------") 
        dump_result = dump_thread.get_result()
        logger.info(dump_result)
        flag = 'dump database ' + self.dbPrimaryUserNode.db_name + ' successfully'
        self.assertFalse(dump_result.find(flag)>-1)
        
        time.sleep(10)
        
        logger.info("-----------dump success--------------------")        
        result = commonshpri.dumpFile(self.dump_file_name, '-F p')
        logger.info(result)
        flag = 'dump database ' + self.dbPrimaryUserNode.db_name + ' successfully'
        self.assertIn(flag, result)
        

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info('----------------delete dump file-----------------------')
        cmd = 'rm -rf ' + self.dump_file_name
        self.dbPrimaryRootNode.sh(cmd)

        
       